<?php
@session_start();
/*
 *  A Part of snock project
 *   Author king-of-war
 * Last modified 2009-4-22
 *
 */
 $root_path='../';
  echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';

 include_once $root_path.'admin/class/pro.class.php';
include_once $root_path.'admin/smarty_config.php';
include_once $root_path.'admin/class/error.class.php';
$error=new Errmsg($root_path);
 $pro=new pro($root_path);

 /*
 *
 * check identify
 */
include_once $root_path.'admin/class/verify.class.php';
$a=new verify($root_path);
$a->identity_check(2);

/*
 *    define file upload moudle
 *
 *
 */
if(isset($_POST['upload'])){
	if(is_uploaded_file($_FILES['upfile']['tmp_name'])){
		$upfile=$_FILES['upfile'];
		$name=$upfile['name'];
		$type=$upfile['type'];
		$size=$upfile['size'];
		$tmp_name=$upfile['tmp_name'];

		switch($type){
			case 'image/pjpeg': $ok=1;
			break;
			case 'image/jpeg': $ok=1;
			break;
			case 'image/gif': $ok=1;
			break;
			case 'image/png': $ok=1;
			break;
			default: $ok=0;
			break;
		}
		if($ok){
if (file_exists('../images/uploads/'.$name)) {
echo '不能重复上传文件！';
}
			move_uploaded_file      ($tmp_name,'../images/uploads/'.$name);
			if($upfile['error']==0){
				$dir='images/uploads/'.$name;
				$image=$dir;
                                echo '上传成功';
			}else{
				echo '上传失败，请检查目录权限';}

		}else{
			echo '不允许此文件类型';}
	}
}

/*
 * get sort comments moudle
 */
 // get the total of sorts
 include_once $root_path.'admin/class/fun_common.php';

$fun=new fun_common($root_path);
 $max=$fun->getAll('products_sort',1);

 // sort operation

 include_once $root_path.'admin/class/sort.class.php';
$sort= new sort($root_path);
$sorts=array();
 // select range te[0] -- te[1]
$te[0]=0;
$te[1]=$max;
$pass=$sort->sort_read($te);

for($i=0;$i<$max;$i++){
	$sorts[$i]['id']=$pass[$i]->get_id();
$sorts[$i]['name']=$pass[$i]->get_name();
}
/* $fun=1 means add
	 * $fun=0 means update
 * define the add
 */
if(isset($_POST['submit'])){
		$name=$_POST['name'];
	$sort_id=$_POST['sort_id'];
	$image=$_POST['image'];
	$intro=$_POST['comments'];
$line=$pro->products_add(null,$sort_id,$image,$name,$intro,1);
if($line){
	$info ='添加成功';
$error->error_chinese($info);
exit;
}else{
		$info ='添加失败';
$error->error_chinese($info);
exit;
}
}

$smarty->assign('sorts',$sorts);
$smarty->assign("dir",$dir);
 $smarty->display("pro.html");
?>
